package huu.phong.googlemusic;

import huu.phong.googlemusic.api.GoogleMusicAPI;
import huu.phong.googlemusic.api.IGoogleMusicAPI;
import huu.phong.googlemusic.model.Song;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class MusicManager {
	public List<Song> songs;
	public List<Album> albums;
	public List<Album> genres;
	public List<Album> artists;
	public List<String> titleAlbums;
	public List<String> titleArtists;
	public List<String> titleGenres;
	public List<Album> currentAlbums;
	public boolean logged = false;
	public static IGoogleMusicAPI googleMusicAPI = new GoogleMusicAPI();
	private String[] ansi = new String[]{"a", "a", "a", "a", "a", "a", "a",
			"a", "a", "a", "a", "a", "a", "a", "a", "o", "o", "o", "o", "o",
			"o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "u", "u", "u",
			"u", "u", "d", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e",
			"i", "i", "i", "i", "i", "u", "u", "u", "u", "u", "y", "y", "y",
			"y", "y", "o", "e", "o", "u", "a", "a"};
	private String[] utf8  = new String[] { "á", "à", "ả", "ạ", "ã", "ắ", "ằ",
					"ặ", "ẳ", "ẵ", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ớ", "ờ", "ợ", "ở", "ỡ",
					"ó", "ò", "ọ", "ỏ", "õ", "ố", "ồ", "ộ", "ổ", "ỗ", "ứ", "ừ", "ự",
					"ử", "ữ", "đ", "é", "è", "ẹ", "ẻ", "ẽ", "ế", "ề", "ệ", "ể", "ễ",
					"í", "ì", "ị", "ỉ", "ĩ", "ú", "ù", "ụ", "ủ", "ũ", "ý", "ỳ", "ỵ",
					"ỷ", "ỹ", "ô", "ê", "ơ", "ư", "ă", "â" };
	
	public MusicManager(){
		songs = new ArrayList<Song>();
		albums = new ArrayList<Album>();
		genres = new ArrayList<Album>();
		artists = new ArrayList<Album>();
		titleAlbums = new ArrayList<String>();
		titleArtists = new ArrayList<String>();
		titleGenres = new ArrayList<String>();
		currentAlbums = new ArrayList<Album>();
	}
	
	public String toANSI(String str){
		for (int i = 0; i < utf8.length; i++) {
			str = str.replaceAll(utf8[i], ansi[i]);
		}
		return str;
	}
	
	public boolean login(String username, String password){
		try {
			googleMusicAPI.login(username, new String(Base64Coder.decode(password)));
		} catch (Exception e) {
			return (logged = false);
		}
		return (logged = true);
	}
	
	public void initSongs() throws IOException, URISyntaxException{
		songs = (List<Song>) googleMusicAPI.getAllSongs();
	}
	
	public void init(){
		albums.clear();
		artists.clear();
		genres.clear();
		titleAlbums.clear();
		titleArtists.clear();
		titleGenres.clear();
		String albumTitle;
		String genreTitle;
		String artistTitle;
		Album album;
		Album genre;
		Album artist;
		int index = -1;
		for (Song song : songs){
			albumTitle = song.getAlbum();
			genreTitle = song.getGenre();
			artistTitle = song.getArtist();
			albumTitle += artistTitle.equals("") ? "" : " - " + artistTitle; 
			
			index = titleAlbums.indexOf(albumTitle);
			if (index == -1){
				album = new Album(albumTitle);
				album.artist = artistTitle;
				albums.add(album);
				titleAlbums.add(albumTitle);
			}else{
				album = albums.get(index);
			}
			album.songs.add(song);
			if (album.albumArt == null && song.getAlbumArtUrl() != null) album.albumArt = "http:" + song.getAlbumArtUrl();
			
			index = titleArtists.indexOf(artistTitle);
			if (index == -1) {
				artist = new Album(artistTitle);
				artists.add(artist);
				titleArtists.add(artistTitle);
			}else{
				artist = artists.get(index);
			}
			artist.songs.add(song);
			if (artist.albumArt == null && song.getAlbumArtUrl() != null) artist.albumArt = "http:" + song.getAlbumArtUrl();
			
			index = titleGenres.indexOf(genreTitle);
			if (index == -1) {
				genre = new Album(genreTitle);
				genres.add(genre);
				titleGenres.add(genreTitle);
			}else{
				genre = genres.get(index);
			}
			genre.songs.add(song);
			if (genre.albumArt == null && song.getAlbumArtUrl() != null) genre.albumArt = "http:" + song.getAlbumArtUrl();
		}
	}
	
	public List<Song> search(String value){
		List<Song> songs = new ArrayList<Song>();
		for (Song song : this.songs){
			if (toANSI((song.getTitle() + " - " + song.getArtist() + " - " + song.getAlbum()).toLowerCase()).contains(toANSI(value.toLowerCase()))){
				songs.add(song);
			}
		}
		return songs;
	}
}
